/*** Trends - Empirical Application

	---- ROBUSTNESS TESTS ----

This code produces table with a set of robustness specifications and a placebo test

1. prepare for robustness tests
2. table with set of robustness tests (Table A.3)
3. placebo test of 2nd generation

**************************************************************************************/


* STATA settings:
clear

* Log file

capture log close
log using ${path2}Logs/${ver}/sensitivity_analyses.log, text replace

/*
0. Baseline
1. Including sibling subsample (=larger sample)
2. Parental age at birth < 30 (=smaller sample)
3. Dropping observations in cohort preceeding reform implementation (=smaller sample)
4. Use max of parental education/income rather than fathers (=larger sample)
5. Father cohort dummies (same sample)
6. Municipality specific trends (same sample)
*/

** 1. Prepare various settings for robustness tests 

use ${path2}Dat/main_file.dta, replace

forval k=0/6 {

* 1a. Baseline

if `k' == 0 {			

cap drop spec1
gen spec1 = 1
replace spec1 = 0 if urval!=1 		// We drop siblings etc
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_2 != 1
replace spec1 = 0 if f_educyrs70==. | f_byear==.

global x_var_spec1 "f_educyrs70"
global y_var_spec1 "educyrs"

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_urval!=1 & m_urval!=1
replace spec2 = 0 if f_byear<1943 | f_byear>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if f_experiment==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_2 != 1
replace spec2 = 0 if f_educyrs90==. | f_byear==.

global x_var_spec2 "f_educyrs90"
global y_var_spec2 "educyrs"
global par_byear_spec2 "f_byear"

cap drop spec3
gen spec3 = 1
replace spec3 = 0 if urval!=1 		// We drop siblings etc
replace spec3 = 0 if foddar<1943 | foddar>1955
replace spec3 = 0 if experiment==. | l_lifeinc3035==. | foddar==.
replace spec3 = 0 if sample_2 != 1
replace spec3 = 0 if f_l_lifeinc5359==. | f_byear==.
replace spec3 = 0 if (1970-f_byear)>55

global y_var_spec3 "l_lifeinc3035"
global x_var_spec3 "f_l_lifeinc5359"

cap drop spec4
gen spec4 = 1
replace spec4 = 0 if f_urval!=1 & m_urval!=1
replace spec4 = 0 if f_byear<1943 | f_byear>1955
replace spec4 = 0 if foddar<1966 | foddar>1972
replace spec4 = 0 if f_experiment==. | l_lifeinc3035==. | foddar==.
replace spec4 = 0 if sample_2 != 1
replace spec4 = 0 if f_l_lifeinc3545==. | f_byear==.

global x_var_spec4 "f_l_lifeinc3545"
global y_var_spec4 "l_lifeinc3035"
global par_byear_spec4 "f_byear"
global ad_ctrls_1 ""
global ad_ctrls_2 ""
global ad_ctrls_3 ""
global ad_ctrls_4 ""
}

* 1b. "1" Including "sibling sample"

if `k' == 1 {			

cap drop spec1
gen spec1 = 1
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_2 != 1
replace spec1 = 0 if f_educyrs70==. | f_byear==.

global x_var_spec1 "f_educyrs70"
global y_var_spec1 "educyrs"

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_byear<1943 | f_byear>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if f_experiment==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_2 != 1
replace spec2 = 0 if f_educyrs90==. | f_byear==.

global x_var_spec2 "f_educyrs90"
global y_var_spec2 "educyrs"
global par_byear_spec2 "f_byear"

cap drop spec3
gen spec3 = 1
replace spec3 = 0 if foddar<1943 | foddar>1955
replace spec3 = 0 if experiment==. | l_lifeinc3035==. | foddar==.
replace spec3 = 0 if sample_2 != 1
replace spec3 = 0 if f_l_lifeinc5359==. | f_byear==.
replace spec3 = 0 if (1970-f_byear)>55
global y_var_spec3 "l_lifeinc3035"
global x_var_spec3 "f_l_lifeinc5359"

cap drop spec4
gen spec4 = 1
replace spec4 = 0 if f_byear<1943 | f_byear>1955
replace spec4 = 0 if foddar<1966 | foddar>1972
replace spec4 = 0 if f_experiment==. | l_lifeinc3035==. | foddar==.
replace spec4 = 0 if sample_2 != 1
replace spec4 = 0 if f_l_lifeinc3545==. | f_byear==.

global x_var_spec4 "f_l_lifeinc3545"
global y_var_spec4 "l_lifeinc3035"
global par_byear_spec4 "f_byear"
}

* 1c. "2" Sample with parental age at birth < 30

if `k' == 2 {			

cap drop spec1
gen spec1 = 1
replace spec1 = 0 if urval!=1 		// We drop siblings etc
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_2 != 1 
replace spec1 = 0 if f_ageatbirth>29 
replace spec1 = 0 if f_educyrs70==. | f_byear==.

global x_var_spec1 "f_educyrs70"
global y_var_spec1 "educyrs"

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_urval!=1 & m_urval!=1
replace spec2 = 0 if f_byear<1943 | f_byear>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if f_experiment==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_2 != 1
replace spec2 = 0 if f_ageatbirth>29 
replace spec2 = 0 if f_educyrs90==. | f_byear==.

global x_var_spec2 "f_educyrs90"
global y_var_spec2 "educyrs"
global par_byear_spec2 "f_byear"

cap drop spec3
gen spec3 = 1
replace spec3 = 0 if urval!=1 		// We drop siblings etc
replace spec3 = 0 if foddar<1943 | foddar>1955
replace spec3 = 0 if experiment==. | l_lifeinc3035==. | foddar==.
replace spec3 = 0 if sample_2 != 1
replace spec3 = 0 if f_ageatbirth>29 
replace spec3 = 0 if f_l_lifeinc5359==. | f_byear==.
replace spec3 = 0 if (1970-f_byear)>55

global y_var_spec3 "l_lifeinc3035"
global x_var_spec3 "f_l_lifeinc5359"

cap drop spec4
gen spec4 = 1
replace spec4 = 0 if f_urval!=1 & m_urval!=1
replace spec4 = 0 if f_byear<1943 | f_byear>1955
replace spec4 = 0 if foddar<1966 | foddar>1972
replace spec4 = 0 if f_experiment==. | l_lifeinc3035==. | foddar==.
replace spec4 = 0 if sample_2 != 1
replace spec4 = 0 if f_ageatbirth>29 
replace spec4 = 0 if f_l_lifeinc3545==. | f_byear==.

global x_var_spec4 "f_l_lifeinc3545"
global y_var_spec4 "l_lifeinc3035"
global par_byear_spec4 "f_byear"
}

* 1d. "3" Dropping cohort preceeding reform

if `k' == 3 {			

cap drop spec1
gen spec1 = 1
replace spec1 = 0 if urval!=1 		// We drop siblings etc
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_2 != 1
replace spec1 = 0 if f_educyrs70==. | f_byear==.
replace spec1 = 0 if foddar == firstcohort60-1

global x_var_spec1 "f_educyrs70"
global y_var_spec1 "educyrs"

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_urval!=1 & m_urval!=1
replace spec2 = 0 if f_byear<1943 | f_byear>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if f_experiment==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_2 != 1
replace spec2 = 0 if f_educyrs90==. | f_byear==.
replace spec2 = 0 if f_byear == f_firstcohort60-1


global x_var_spec2 "f_educyrs90"
global y_var_spec2 "educyrs"
global par_byear_spec2 "f_byear"

cap drop spec3
gen spec3 = 1
replace spec3 = 0 if urval!=1 		// We drop siblings etc
replace spec3 = 0 if foddar<1943 | foddar>1955
replace spec3 = 0 if experiment==. | l_lifeinc3035==. | foddar==.
replace spec3 = 0 if sample_2 != 1
replace spec3 = 0 if f_l_lifeinc5359==. | f_byear==.
replace spec3 = 0 if foddar == firstcohort60-1
replace spec3 = 0 if (1970-f_byear)>55


global y_var_spec3 "l_lifeinc3035"
global x_var_spec3 "f_l_lifeinc5359"

cap drop spec4
gen spec4 = 1
replace spec4 = 0 if f_urval!=1 & m_urval!=1
replace spec4 = 0 if f_byear<1943 | f_byear>1955
replace spec4 = 0 if foddar<1966 | foddar>1972
replace spec4 = 0 if f_experiment==. | l_lifeinc3035==. | foddar==.
replace spec4 = 0 if sample_2 != 1
replace spec4 = 0 if f_l_lifeinc3545==. | f_byear==.
replace spec4 = 0 if f_byear == f_firstcohort60-1


global x_var_spec4 "f_l_lifeinc3545"
global y_var_spec4 "l_lifeinc3035"
global par_byear_spec4 "f_byear"
}


* 1e. "4"  Use max of parental education/income rather than fathers

if `k' == 4 {			

cap drop spec1
gen spec1 = 1
replace spec1 = 0 if urval!=1 		// We drop siblings etc
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_4_ed != 1
replace spec1 = 0 if p_educyrs==. | p_byear_ed==.

global x_var_spec1 "p_educyrs"
global y_var_spec1 "educyrs"

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_urval!=1 & m_urval!=1
replace spec2 = 0 if p_byear_ed<1943 | p_byear_ed>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if p_experiment_ed==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_4_ed != 1
replace spec2 = 0 if p_educyrs==. | p_byear_ed==.

global x_var_spec2 "p_educyrs"
global y_var_spec2 "educyrs"
global par_byear_spec2 "p_byear_ed"

cap drop spec3
gen spec3 = 1
replace spec3 = 0 if urval!=1 		// We drop siblings etc
replace spec3 = 0 if foddar<1943 | foddar>1955
replace spec3 = 0 if experiment==. | l_lifeinc3035==. | foddar==.
replace spec3 = 0 if sample_4_inc1 != 1
replace spec3 = 0 if p_l_lifeinc5359==. | p_byear_inc1==.
replace spec3 = 0 if (1970-p_byear_inc1)>55

global y_var_spec3 "l_lifeinc3035"
global x_var_spec3 "p_l_lifeinc5359"

cap drop spec4
gen spec4 = 1
replace spec4 = 0 if f_urval!=1 & m_urval!=1
replace spec4 = 0 if p_byear_inc2<1943 | p_byear_inc2>1955
replace spec4 = 0 if foddar<1966 | foddar>1972
replace spec4 = 0 if p_experiment_inc2==. | l_lifeinc3035==. | foddar==.
replace spec4 = 0 if sample_4_inc2 != 1
replace spec4 = 0 if p_l_lifeinc3545==. | p_byear_inc2==.

global x_var_spec4 "p_l_lifeinc3545"
global y_var_spec4 "l_lifeinc3035"
global par_byear_spec4 "p_byear_inc2"
}

* 1f. "5" Father cohort dummies

if `k' == 5 {			

cap drop spec1
gen spec1 = 1
replace spec1 = 0 if urval!=1 		// We drop siblings etc
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_2 != 1
replace spec1 = 0 if f_educyrs70==. | f_byear==.

global x_var_spec1 "f_educyrs70"
global y_var_spec1 "educyrs"
global par_byear_spec1 "f_byear"

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_urval!=1 & m_urval!=1
replace spec2 = 0 if f_byear<1943 | f_byear>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if f_experiment==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_2 != 1
replace spec2 = 0 if f_educyrs90==. | f_byear==.

global x_var_spec2 "f_educyrs90"
global y_var_spec2 "educyrs"
global par_byear_spec2 "f_byear"

cap drop spec3
gen spec3 = 1
replace spec3 = 0 if urval!=1 		// We drop siblings etc
replace spec3 = 0 if foddar<1943 | foddar>1955
replace spec3 = 0 if experiment==. | l_lifeinc3035==. | foddar==.
replace spec3 = 0 if sample_2 != 1
replace spec3 = 0 if f_l_lifeinc5359==. | f_byear==.

global y_var_spec3 "l_lifeinc3035"
global x_var_spec3 "f_l_lifeinc5359"
global par_byear_spec3 "f_byear"

cap drop spec4
gen spec4 = 1
replace spec4 = 0 if f_urval!=1 & m_urval!=1
replace spec4 = 0 if f_byear<1943 | f_byear>1955
replace spec4 = 0 if foddar<1966 | foddar>1972
replace spec4 = 0 if f_experiment==. | l_lifeinc3035==. | foddar==.
replace spec4 = 0 if sample_2 != 1
replace spec4 = 0 if f_l_lifeinc3545==. | f_byear==.

global x_var_spec4 "f_l_lifeinc3545"
global y_var_spec4 "l_lifeinc3035"
global par_byear_spec4 "f_byear"
global ad_ctrls_1 "i.${par_byear_spec1} i.${par_byear_spec1}#c.${x_var_spec1}"
global ad_ctrls_2 "i.foddar i.foddar#c.${x_var_spec2}"
global ad_ctrls_3 "i.${par_byear_spec3} i.${par_byear_spec3}#c.${x_var_spec3}"
global ad_ctrls_4 "i.foddar i.foddar#c.${x_var_spec4}"
}

* 1g. "6" Linear municipality-specific time trends

if `k' == 6 {			

cap drop spec1
gen spec1 = 1
replace spec1 = 0 if urval!=1 		// We drop siblings etc
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_2 != 1
replace spec1 = 0 if f_educyrs70==. | f_byear==.

global x_var_spec1 "f_educyrs70"
global y_var_spec1 "educyrs"
global par_byear_spec1 "f_byear"

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_urval!=1 & m_urval!=1
replace spec2 = 0 if f_byear<1943 | f_byear>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if f_experiment==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_2 != 1
replace spec2 = 0 if f_educyrs90==. | f_byear==.

global x_var_spec2 "f_educyrs90"
global y_var_spec2 "educyrs"
global par_byear_spec2 "f_byear"

cap drop spec3
gen spec3 = 1
replace spec3 = 0 if urval!=1 		// We drop siblings etc
replace spec3 = 0 if foddar<1943 | foddar>1955
replace spec3 = 0 if experiment==. | l_lifeinc3035==. | foddar==.
replace spec3 = 0 if sample_2 != 1
replace spec3 = 0 if f_l_lifeinc5359==. | f_byear==.

global y_var_spec3 "l_lifeinc3035"
global x_var_spec3 "f_l_lifeinc5359"
global par_byear_spec3 "f_byear"

cap drop spec4
gen spec4 = 1
replace spec4 = 0 if f_urval!=1 & m_urval!=1
replace spec4 = 0 if f_byear<1943 | f_byear>1955
replace spec4 = 0 if foddar<1966 | foddar>1972
replace spec4 = 0 if f_experiment==. | l_lifeinc3035==. | foddar==.
replace spec4 = 0 if sample_2 != 1
replace spec4 = 0 if f_l_lifeinc3545==. | f_byear==.

cap drop lintrend 
cap drop f_lintrend
gen lintrend = foddar-1942
gen f_lintrend = f_byear-1942
global x_var_spec4 "f_l_lifeinc3545"
global y_var_spec4 "l_lifeinc3035"
global par_byear_spec4 "f_byear"
global ad_ctrls_1 "c.lintrend#i.kommun60_"
global ad_ctrls_2 "c.f_lintrend#i.f_kommun60_"
global ad_ctrls_3 "c.lintrend#i.kommun60_"
global ad_ctrls_4 "c.f_lintrend#i.f_kommun60_"
}

** 2. Regressions and create table with tests (Table A.3)

* 2a. First generation, education
	set more off
	areg ${y_var_spec1} i.foddar#c.${x_var_spec1} i.kommun60_#c.${x_var_spec1} ${x_var_spec1} experiment c.experiment#c.${x_var_spec1} i.foddar ${ad_ctrls_1} if spec1==1, absorb(kommun60_) vce(cluster kommun60_)		
	
		di "Sensitivity analysis (1st gen, educ, analysis `k'): "
		di "experiment    " "_b " _b[experiment] " _se " _se[experiment] 
		di "${x_var_spec1}     " "_b " _b[${x_var_spec1}] " _se " _se[${x_var_spec1}]
		di "c.experiment#c.${x_var_spec1} " "_b " _b[c.experiment#c.${x_var_spec1}] " _se " _se[c.experiment#c.${x_var_spec1}]
	
	scalar b_spec1_`k'=_b[c.experiment#c.${x_var_spec1}]
	scalar se_spec1_`k'=_se[c.experiment#c.${x_var_spec1}]

* 2b. Second generation, education

	if `k' != 4	{
	set more off
	qui areg ${y_var_spec2} i.${par_byear_spec2} i.f_kommun60_#c.${x_var_spec2} i.${par_byear_spec2}#c.${x_var_spec2} ${x_var_spec2} f_experiment c.f_experiment#c.${x_var_spec2} ${ad_ctrls_2} if spec2==1, absorb(f_kommun60_) vce(cluster f_kommun60_)

	di "Sensitivity analysis (2nd gen, educ, analysis `k'): "
		di "f_experiment  " "_b " _b[f_experiment] " _se " _se[f_experiment] 
		di "${x_var_spec2}     " "_b " _b[${x_var_spec2}] " _se " _se[${x_var_spec2}]
		di "c.f_experiment#c.${x_var_spec2} " "_b " _b[c.f_experiment#c.${x_var_spec2}] " _se " _se[c.f_experiment#c.${x_var_spec2}]
		
	scalar b_spec2_`k'=_b[c.f_experiment#c.${x_var_spec2}]
	scalar se_spec2_`k'=_se[c.f_experiment#c.${x_var_spec2}]
	}
	
	if `k' == 4	{
	qui areg ${y_var_spec2} i.${par_byear_spec2} i.p_kommun60_ed#c.${x_var_spec2} i.${par_byear_spec2}#c.${x_var_spec2} ${x_var_spec2} p_experiment_ed c.p_experiment_ed#c.${x_var_spec2} ${ad_ctrls_2} if spec2==1, absorb(p_kommun60_ed) vce(cluster p_kommun60_ed)

	di "Sensitivity analysis (2nd gen, educ, analysis `k'): "
		di "p_experiment_ed  " "_b " _b[p_experiment_ed] " _se " _se[p_experiment_ed] 
		di "${x_var_spec2}     " "_b " _b[${x_var_spec2}] " _se " _se[${x_var_spec2}]
		di "c.p_experiment_ed#c.${x_var_spec2} " "_b " _b[c.p_experiment_ed#c.${x_var_spec2}] " _se " _se[c.p_experiment_ed#c.${x_var_spec2}]
		
	scalar b_spec2_`k'=_b[c.p_experiment_ed#c.${x_var_spec2}]
	scalar se_spec2_`k'=_se[c.p_experiment_ed#c.${x_var_spec2}]
	}

* 2c. First generation, income
set more off
	areg ${y_var_spec3} i.foddar#c.${x_var_spec3} i.kommun60_#c.${x_var_spec3} ${x_var_spec3} experiment c.experiment#c.${x_var_spec3} i.foddar ${ad_ctrls_3} if spec3==1 & ${x_var_spec3}>log(10000) & ${y_var_spec3}>log(10000), absorb(kommun60_) vce(cluster kommun60_)

	di "Sensitivity analysis (1st gen, income, analysis `k'): "
		di "experiment    " "_b " _b[experiment] " _se " _se[experiment] 
		di "${x_var_spec3}     " "_b " _b[${x_var_spec3}] " _se " _se[${x_var_spec3}]
		di "c.experiment#c.${x_var_spec3} " "_b " _b[c.experiment#c.${x_var_spec3}] " _se " _se[c.experiment#c.${x_var_spec3}]
	
	scalar b_spec3_`k'=_b[c.experiment#c.${x_var_spec3}]
	scalar se_spec3_`k'=_se[c.experiment#c.${x_var_spec3}]

* 2d. Second generation, income

	if `k' != 4	{
	set more off
	qui areg ${y_var_spec4} i.${par_byear_spec4} i.f_kommun60_#c.${x_var_spec4} i.${par_byear_spec4}#c.${x_var_spec4} ${x_var_spec4} f_experiment c.f_experiment#c.${x_var_spec4} ${ad_ctrls_4} if spec4==1 &  ${x_var_spec4}>log(10000) & ${y_var_spec4}>log(10000), absorb(f_kommun60_) vce(cluster f_kommun60_)

	di "Sensitivity analysis (2nd gen, income, analysis `k'): "
		di "f_experiment  " "_b " _b[f_experiment] " _se " _se[f_experiment] 
		di "${x_var_spec4}     " "_b " _b[${x_var_spec4}] " _se " _se[${x_var_spec4}]
		di "c.f_experiment#c.${x_var_spec4} " "_b " _b[c.f_experiment#c.${x_var_spec4}] " _se " _se[c.f_experiment#c.${x_var_spec4}]
		
	scalar b_spec4_`k'=_b[c.f_experiment#c.${x_var_spec4}]
	scalar se_spec4_`k'=_se[c.f_experiment#c.${x_var_spec4}]
	}
	if `k' == 4	{
	qui areg ${y_var_spec4} i.${par_byear_spec4} i.p_kommun60_inc2#c.${x_var_spec4} i.${par_byear_spec4}#c.${x_var_spec4} ${x_var_spec4} p_experiment_inc2 c.p_experiment_inc2#c.${x_var_spec4} ${ad_ctrls_4} if spec4==1 &  ${x_var_spec4}>log(10000) & ${y_var_spec4}>log(10000), absorb(p_kommun60_inc2) vce(cluster p_kommun60_inc2)

	di "Sensitivity analysis (2nd gen, income, analysis `k'): "
		di "p_experiment_inc2  " "_b " _b[p_experiment_inc2] " _se " _se[p_experiment_inc2] 
		di "${x_var_spec4}     " "_b " _b[${x_var_spec4}] " _se " _se[${x_var_spec4}]
		di "c.p_experiment_inc2#c.${x_var_spec4} " "_b " _b[c.p_experiment_inc2#c.${x_var_spec4}] " _se " _se[c.p_experiment_inc2#c.${x_var_spec4}]
		
	scalar b_spec4_`k'=_b[c.p_experiment_inc2#c.${x_var_spec4}]
	scalar se_spec4_`k'=_se[c.p_experiment_inc2#c.${x_var_spec4}]
	}
}

* 2e. Construct table with results

mat sens_table=J(8,7,.)
forval k = 0/6 {
local c = `k'+1
mat sens_table[1,`c']=b_spec1_`k'
mat sens_table[2,`c']=se_spec1_`k'
mat sens_table[3,`c']=b_spec2_`k'
mat sens_table[4,`c']=se_spec2_`k'
mat sens_table[5,`c']=b_spec3_`k'
mat sens_table[6,`c']=se_spec3_`k'
mat sens_table[7,`c']=b_spec4_`k'
mat sens_table[8,`c']=se_spec4_`k'
}

mat coln sens_table = test_0 test_1 test_2 test_3 test_4 test_5 test_6
mat rown sens_table = b_spec1 se_spec1 b_spec2 se_spec2 b_spec3 se_spec3 b_spec4 se_spec4

outtable using "${path2}/Logs/${ver}/sensitivity_analyses" , mat(sens_table) nobox format(%9.4f) caption(Sensitivity analyses)center replace

** 3. Placebo test of common trends 

cap drop spec1 
cap drop specm1
gen spec1 = 1
replace spec1 = 0 if urval!=1 		// We drop siblings etc
replace spec1 = 0 if foddar<1943 | foddar>1955
replace spec1 = 0 if experiment==. | educyrs==. | foddar==.
replace spec1 = 0 if sample_2 != 1
replace spec1 = 0 if f_educyrs70==. | f_byear==.
gen specm1 = spec1

global ad_ctrls_1 ""
global ad_ctrls_2 ""
global ad_ctrls_3 ""
global ad_ctrls_4 ""

cap drop placebo_experiment_*
replace spec1 = 0 if experiment==0
gen placebo_experiment_p1 = experiment
replace placebo_experiment_p1 = 0 if firstcohort60==foddar
gen placebo_experiment_p2 = placebo_experiment_p1 
replace placebo_experiment_p2 = 0 if foddar-firstcohort60==1
gen placebo_experiment_p3 = placebo_experiment_p2 
replace placebo_experiment_p3 = 0 if foddar-firstcohort60==2
gen placebo_experiment_p4 = placebo_experiment_p3 
replace placebo_experiment_p4 = 0 if foddar-firstcohort60==3
gen placebo_experiment_p5 = placebo_experiment_p4 
replace placebo_experiment_p5 = 0 if foddar-firstcohort60==4


replace specm1 = 0 if experiment==1
gen placebo_experiment_m1 = experiment
replace placebo_experiment_m1 = 1 if foddar-firstcohort60==-1
gen placebo_experiment_m2 = placebo_experiment_m1 
replace placebo_experiment_m2 = 1 if foddar-firstcohort60==-2
gen placebo_experiment_m3 = placebo_experiment_m2 
replace placebo_experiment_m3 = 1 if foddar-firstcohort60==-3
gen placebo_experiment_m4 = placebo_experiment_m3 
replace placebo_experiment_m4 = 1 if foddar-firstcohort60==-4
gen placebo_experiment_m5 = placebo_experiment_m4 
replace placebo_experiment_m5 = 1 if foddar-firstcohort60==-5


global x_var_spec1 "f_educyrs70"
global y_var_spec1 "educyrs"

forval k = 1/5 {
set more off
	qui areg ${y_var_spec1} i.foddar#c.${x_var_spec1} i.kommun60_#c.${x_var_spec1} ${x_var_spec1} placebo_experiment_p`k' c.placebo_experiment_p`k'#c.${x_var_spec1} i.foddar if spec1==1, absorb(kommun60_) vce(cluster kommun60_)		
	
		di "Sensitivity analysis (Placebo plus years, 1st gen, educ, analysis `k'): "
		di "experiment    " "_b " _b[placebo_experiment_p`k'] " _se " _se[placebo_experiment_p`k'] 
		di "${x_var_spec1}     " "_b " _b[${x_var_spec1}] " _se " _se[${x_var_spec1}]
		di "c.placebo_experiment_p`k'#c.${x_var_spec1} " "_b " _b[c.placebo_experiment_p`k'#c.${x_var_spec1}] " _se " _se[c.placebo_experiment_p`k'#c.${x_var_spec1}]
	
	scalar b_spec1_placebo_p`k'=_b[c.placebo_experiment_p`k'#c.${x_var_spec1}]
	scalar se_spec1_placebo_p`k'=_se[c.placebo_experiment_p`k'#c.${x_var_spec1}]
	
	qui areg ${y_var_spec1} i.foddar#c.${x_var_spec1} i.kommun60_#c.${x_var_spec1} ${x_var_spec1} placebo_experiment_m`k' c.placebo_experiment_m`k'#c.${x_var_spec1} i.foddar if specm1==1, absorb(kommun60_) vce(cluster kommun60_)		
	
		di "Sensitivity analysis (Placebo minus years, 1st gen, educ, analysis `k'): "
		di "experiment    " "_b " _b[placebo_experiment_m`k'] " _se " _se[placebo_experiment_m`k'] 
		di "${x_var_spec1}     " "_b " _b[${x_var_spec1}] " _se " _se[${x_var_spec1}]
		di "c.placebo_experiment_m`k'#c.${x_var_spec1} " "_b " _b[c.placebo_experiment_m`k'#c.${x_var_spec1}] " _se " _se[c.placebo_experiment_m`k'#c.${x_var_spec1}]
	
	scalar b_spec1_placebo_m`k'=_b[c.placebo_experiment_m`k'#c.${x_var_spec1}]
	scalar se_spec1_placebo_m`k'=_se[c.placebo_experiment_m`k'#c.${x_var_spec1}]
}

cap drop spec2
gen spec2 = 1
replace spec2 = 0 if f_urval!=1 & m_urval!=1
replace spec2 = 0 if f_byear<1943 | f_byear>1955
replace spec2 = 0 if foddar<1966 | foddar>1972
replace spec2 = 0 if f_experiment==. | educyrs==. | foddar==.
replace spec2 = 0 if sample_2 != 1
replace spec2 = 0 if f_educyrs90==. | f_byear==.
cap drop specm2
gen specm2= spec2

global x_var_spec2 "f_educyrs90"
global y_var_spec2 "educyrs"
global par_byear_spec2 "f_byear"

cap drop placebo_f_experiment_*
replace spec2 = 0 if f_experiment==0
gen placebo_f_experiment_p1 = f_experiment
replace placebo_f_experiment_p1 = 0 if f_firstcohort60==f_byear
gen placebo_f_experiment_p2 = placebo_f_experiment_p1 
replace placebo_f_experiment_p2 = 0 if f_byear-f_firstcohort60==1
gen placebo_f_experiment_p3 = placebo_f_experiment_p2 
replace placebo_f_experiment_p3 = 0 if f_byear-f_firstcohort60==2
gen placebo_f_experiment_p4 = placebo_f_experiment_p3 
replace placebo_f_experiment_p4 = 0 if f_byear-f_firstcohort60==3
gen placebo_f_experiment_p5 = placebo_f_experiment_p4 
replace placebo_f_experiment_p5 = 0 if f_byear-f_firstcohort60==4


replace specm2 = 0 if f_experiment==1
gen placebo_f_experiment_m1 = f_experiment
replace placebo_f_experiment_m1 = 1 if f_byear-f_firstcohort60==-1
gen placebo_f_experiment_m2 = placebo_f_experiment_m1 
replace placebo_f_experiment_m2 = 1 if f_byear-f_firstcohort60==-2
gen placebo_f_experiment_m3 = placebo_f_experiment_m2 
replace placebo_f_experiment_m3 = 1 if f_byear-f_firstcohort60==-3
gen placebo_f_experiment_m4 = placebo_f_experiment_m3 
replace placebo_f_experiment_m4 = 1 if f_byear-f_firstcohort60==-4
gen placebo_f_experiment_m5 = placebo_f_experiment_m4 
replace placebo_f_experiment_m5 = 1 if f_byear-f_firstcohort60==-5

forval k = 1/5 {
	qui areg ${y_var_spec2} i.${par_byear_spec2} i.f_kommun60_#c.${x_var_spec2} i.${par_byear_spec2}#c.${x_var_spec2} ${x_var_spec2} placebo_f_experiment_p`k' c.placebo_f_experiment_p`k'#c.${x_var_spec2} if spec2==1, absorb(f_kommun60_) vce(cluster f_kommun60_)

	di "Sensitivity analysis (2nd gen, educ, analysis `k'): "
		di "placebo_f_experiment_p`k'  " "_b " _b[placebo_f_experiment_p`k'] " _se " _se[placebo_f_experiment_p`k'] 
		di "${x_var_spec2}     " "_b " _b[${x_var_spec2}] " _se " _se[${x_var_spec2}]
		di "c.placebo_f_experiment_p`k'#c.${x_var_spec2} " "_b " _b[c.placebo_f_experiment_p`k'#c.${x_var_spec2}] " _se " _se[c.placebo_f_experiment_p`k'#c.${x_var_spec2}]
		
	scalar b_spec2_placebo_p`k'=_b[c.placebo_f_experiment_p`k'#c.${x_var_spec2}]
	scalar se_spec2_placebo_p`k'=_se[c.placebo_f_experiment_p`k'#c.${x_var_spec2}]
	
	qui areg ${y_var_spec2} i.${par_byear_spec2} i.f_kommun60_#c.${x_var_spec2} i.${par_byear_spec2}#c.${x_var_spec2} ${x_var_spec2} placebo_f_experiment_m`k' c.placebo_f_experiment_m`k'#c.${x_var_spec2} if specm2==1, absorb(f_kommun60_) vce(cluster f_kommun60_)

	di "Sensitivity analysis (Placebo minus years, 2nd gen, educ, analysis `k'): "
		di "placebo_f_experiment_m`k'  " "_b " _b[placebo_f_experiment_m`k'] " _se " _se[placebo_f_experiment_m`k'] 
		di "${x_var_spec2}     " "_b " _b[${x_var_spec2}] " _se " _se[${x_var_spec2}]
		di "c.placebo_f_experiment_m`k'#c.${x_var_spec2} " "_b " _b[c.placebo_f_experiment_m`k'#c.${x_var_spec2}] " _se " _se[c.placebo_f_experiment_m`k'#c.${x_var_spec2}]
		
	scalar b_spec2_placebo_m`k'=_b[c.placebo_f_experiment_m`k'#c.${x_var_spec2}]
	scalar se_spec2_placebo_m`k'=_se[c.placebo_f_experiment_m`k'#c.${x_var_spec2}]
}

* graph placebo

cap drop placebo_year
cap drop placebo_ed*
gen placebo_year = .
gen placebo_ed1_b = .
gen placebo_ed1_se =.
gen placebo_ed2_b =.
gen placebo_ed2_se =.

replace placebo_year = 0 in 6
replace placebo_ed1_b = b_spec1_0 in 6
replace placebo_ed1_se = se_spec1_0 in 6
replace placebo_ed2_b = b_spec2_0 in 6
replace placebo_ed2_se = se_spec2_0 in 6

forval k = 1/5 {
local y = 6 - `k'
replace placebo_year = -`k' in `y'
replace placebo_ed1_b = b_spec1_placebo_m`k' in `y'
replace placebo_ed1_se = se_spec1_placebo_m`k' in `y'
replace placebo_ed2_b = b_spec2_placebo_m`k' in `y'
replace placebo_ed2_se = se_spec2_placebo_m`k' in `y'
}

forval k = 1/5 {
local y = `k'+6
replace placebo_year = `k' in `y'
replace placebo_ed1_b = b_spec1_placebo_p`k' in `y'
replace placebo_ed1_se = se_spec1_placebo_p`k' in `y'
replace placebo_ed2_b = b_spec2_placebo_p`k' in `y'
replace placebo_ed2_se = se_spec2_placebo_p`k' in `y'
}


gen placebo_ed1_max95 = placebo_ed1_b+placebo_ed1_se*1.96
gen placebo_ed2_max95 = placebo_ed2_b+placebo_ed2_se*1.96
gen placebo_ed1_min95 = placebo_ed1_b-placebo_ed1_se*1.96
gen placebo_ed2_min95 = placebo_ed2_b-placebo_ed2_se*1.96


twoway (scatter placebo_ed1_b placebo_year, sort lcolor(black)) (rcap placebo_ed1_max95 placebo_ed1_min95 placebo_year, lcolor(black)),  /// 
	scheme(s2mono) graphregion(color(white)) ytitle("Coefficient and confidence interval") legend(off) ylabel(-.08(0.02).08) xtitle("Year of placebo vs. true reform") xlabel(-5(1)5) yline(0, lcolor(red)) xline(0, lcolor(gray) lpattern(dash)) 
	graph save ${path2}Logs/${ver}/placebo_reform_ed1.gph, replace
	qui graph export ${path2}Logs/${ver}/placebo_reform_ed1.eps , replace
	
twoway (scatter placebo_ed2_b placebo_year, sort lcolor(black)) (rcap placebo_ed2_max95 placebo_ed2_min95 placebo_year, lcolor(black)),  /// 
	scheme(s2mono) graphregion(color(white)) ytitle("Coefficient and confidence interval") legend(off) ylabel(-.08(0.02).08) xtitle("Year of placebo vs. true reform") xlabel(-5(1)5) yline(0, lcolor(red)) xline(0, lcolor(gray) lpattern(dash)) 
	graph save ${path2}Logs/${ver}/placebo_reform_ed2, replace
	qui graph export ${path2}Logs/${ver}/placebo_reform_ed2.eps , replace

cap log close
